//------------------------------------------------------------------------------
#include <QMessageBox>
#include "CSelectTypeFactureFrm.h"
#include "CComboResultFrm.h"
#include "CMySQL.h"
//------------------------------------------------------------------------------
CSelectTypeFactureFrm::CSelectTypeFactureFrm(QWidget *parent) : QDialog(parent) {
	setupUi(this);
	devisSelected=false;
	commandeSelected=false;
}
//------------------------------------------------------------------------------
void CSelectTypeFactureFrm::onComboDevis(CMyComboBox *comboBox) {
	CComboResultFrm *comboResultFrm=new CComboResultFrm(comboBox->getTableRemp(), comboBox->getChampRemp(), this);
	
	comboResultFrm->setModal(true);
	comboResultFrm->exec();
	
	if(comboResultFrm->result() == QDialog::Accepted) {
		infoDevis=QMap<QString, QString>(comboResultFrm->getResults());
		devis->setValue(infoDevis.value("teteDevis.nomDevis"));
	}
	
	delete comboResultFrm;
}
//------------------------------------------------------------------------------
void CSelectTypeFactureFrm::onComboCommande(CMyComboBox *comboBox)
{
	CComboResultFrm *comboResultFrm=new CComboResultFrm(comboBox->getTableRemp(), comboBox->getChampRemp(), this);
	
	comboResultFrm->setModal(true);
	comboResultFrm->exec();
	
	if(comboResultFrm->result() == QDialog::Accepted) {
		infoCommande=QMap<QString, QString>(comboResultFrm->getResults());
		commande->setValue(infoCommande.value("teteCommandes.nomCommande"));
	}
	
	delete comboResultFrm;
}
//------------------------------------------------------------------------------
void CSelectTypeFactureFrm::onComboClient(CMyComboBox *comboBox)
{
	CComboResultFrm *comboResultFrm=new CComboResultFrm(comboBox->getTableRemp(), comboBox->getChampRemp(), this);
	
	comboResultFrm->setModal(true);
	comboResultFrm->exec();
	
	if(comboResultFrm->result() == QDialog::Accepted)
	{
		comboBox->setValue(comboResultFrm->getResult());
		_client=comboResultFrm->getResult();
	}
	
	delete comboResultFrm;
}
//------------------------------------------------------------------------------
void CSelectTypeFactureFrm::on_optionSimple_clicked(bool checked) {
	_client="";
	devis->setEnabled(false);
	devisSelected=false;
	commande->setEnabled(false);
	commandeSelected=false;
	client->setEnabled(false);
}
//------------------------------------------------------------------------------
void CSelectTypeFactureFrm::on_optionDevis_clicked(bool checked) {
	_client="";
	devis->setEnabled(true);
	devisSelected=true;
	commande->setEnabled(false);
	commandeSelected=false;
	client->setEnabled(false);
}
//------------------------------------------------------------------------------
void CSelectTypeFactureFrm::on_optionCommande_clicked(bool checked) {
	_client="";
	devis->setEnabled(false);
	devisSelected=false;
	commande->setEnabled(true);
	commandeSelected=true;
	client->setEnabled(false);
}
//------------------------------------------------------------------------------
void CSelectTypeFactureFrm::on_optionClient_clicked(bool checked)
{
	_client=client->getValue();
	devis->setEnabled(false);
	devisSelected=false;
	commande->setEnabled(false);
	commandeSelected=false;
	client->setEnabled(true);
}
//------------------------------------------------------------------------------
void CSelectTypeFactureFrm::accept(void) {
	if(optionDevis->isChecked()) {
		if(!CMySQL::getInstance()->existe("teteDevis", "nomDevis", devis->getValue())) {
			QMessageBox msgBox(	QMessageBox::Warning, QObject::tr("Erreur"), 
								tr("Le devis '")+devis->getValue()+tr("' n'existe pas, impossible de créer la facture"), QMessageBox::Ok);
			msgBox.exec();
			
			return;
		}
	}
	
	if(optionCommande->isChecked()) {
		if(!CMySQL::getInstance()->existe("teteCommandes", "nomCommande", commande->getValue())) {
			QMessageBox msgBox(	QMessageBox::Warning, QObject::tr("Erreur"), 
								tr("La commande '")+commande->getValue()+tr("' n'existe pas, impossible de créer la facture"), QMessageBox::Ok);
			msgBox.exec();
			
			return;
		}
	}
	
	if(optionClient->isChecked()) {
		if(!CMySQL::getInstance()->existe("client", "code", client->getValue())) {
			QMessageBox msgBox(	QMessageBox::Warning, QObject::tr("Erreur"), 
								tr("Le client '")+client->getValue()+tr("' n'existe pas, impossible de créer la facture"), QMessageBox::Ok);
			msgBox.exec();
			
			return;
		}
		_client=client->getValue();
	}
	
	QDialog::accept();
}
//------------------------------------------------------------------------------